package lib.persistence.commands;

import global.ConfigData;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Date;

import lib.persistence.NotFoundException;
import lib.persistence.commands.GetRow;
import models.*;


/**
 * this class is for grabbing items from the database based on if they have a certain field matching a "search term"
 * @author ben
 *
 */
public class GetModelsByField {
	public ArrayList<Event> getEventByName(String LikeStr) throws SQLException, IOException, NotFoundException, ClassNotFoundException
	{
		ArrayList<Object> out = new ArrayList<Object>();
		ArrayList<Event> returnResult = new ArrayList<Event>();
		ArrayList<Event> FinalResult = new ArrayList<Event>();
		GetRow results = new GetRow();
		out = results.findObjects(ConfigData.EVENT_TABLE, "obj");
		//convert results from Objects to Model type
		for(int i = 0; i < out.size(); i++)
		{
			returnResult.add((Event) out.get(i));
		}
		//Now only get the records who have a match to "LikeStr"
		for(int i = 0; i < returnResult.size(); i++)
		{
			Event temp = returnResult.get(i);
			if(temp.getEventName().compareToIgnoreCase(LikeStr) != 0){
				returnResult.remove(i);
				i--;
			}
		}
		//Now we check for referential Integrity.
		for(int i = 0; i < returnResult.size(); i++)
		{
			Event e; 
			int id = returnResult.get(i).getId();
			e = Event.find(id);
			FinalResult.add(e);
		}
		return FinalResult;
	}
	
	public ArrayList<Event> getEventByStatus(String LikeStr) throws SQLException, IOException, NotFoundException, ClassNotFoundException
	{
		ArrayList<Object> out = new ArrayList<Object>();
		ArrayList<Event> returnResult = new ArrayList<Event>();
		ArrayList<Event> FinalResult = new ArrayList<Event>();
		GetRow results = new GetRow();
		out = results.findObjects(ConfigData.EVENT_TABLE, "obj");
		//convert results from Objects to Model type
		for(int i = 0; i < out.size(); i++)
		{
			returnResult.add((Event) out.get(i));
		}
		//Now only get the records who have a match to "LikeStr"
		for(int i = 0; i < returnResult.size(); i++)
		{
			Event temp = returnResult.get(i);
			if(temp.getStatus().toString().compareToIgnoreCase(LikeStr) != 0){
				returnResult.remove(i);
				i--;
			}
		}
		//Now we check for referential Integrity.
		for(int i = 0; i < returnResult.size(); i++)
		{
			Event e; 
			int id = returnResult.get(i).getId();
			e = Event.find(id);
			FinalResult.add(e);
		}
		return FinalResult;
	}
	
	public ArrayList<Event> getEventByDate(Date LikeDate) throws SQLException, IOException, NotFoundException, ClassNotFoundException
	{
		ArrayList<Object> out = new ArrayList<Object>();
		ArrayList<Event> returnResult = new ArrayList<Event>();
		ArrayList<Event> FinalResult = new ArrayList<Event>();
		GetRow results = new GetRow();
		out = results.findObjects(ConfigData.EVENT_TABLE, "obj");
		//convert results from Objects to Model type
		for(int i = 0; i < out.size(); i++)
		{
			returnResult.add((Event) out.get(i));
		}
		//Now only get the records who have a match to "LikeDate"
		for(int i = 0; i < returnResult.size(); i++)
		{
			Event temp = returnResult.get(i);
			if(!temp.getEventDate().equals(LikeDate)){
				returnResult.remove(i);
				i--;
			}
		}
		//Now we check for referential Integrity.
		for(int i = 0; i < returnResult.size(); i++)
		{
			Event e; 
			int id = returnResult.get(i).getId();
			e = Event.find(id);
			FinalResult.add(e);
		}
		return FinalResult;
	}
	
	public ArrayList<Person> getPersonByName(String LikeStr) throws SQLException, IOException, NotFoundException, ClassNotFoundException
	{
		ArrayList<Object> out = new ArrayList<Object>();
		ArrayList<Person> returnResult = new ArrayList<Person>();
		ArrayList<Person> FinalResult = new ArrayList<Person>();
		GetRow results = new GetRow();
		out = results.findObjects(ConfigData.PERSON_TABLE, "obj");
		//convert results from Objects to Model type
		for(int i = 0; i < out.size(); i++)
		{
			returnResult.add((Person) out.get(i));
		}
		//Now only get the records who have a match to "LikeStr"
		for(int i = 0; i < returnResult.size(); i++)
		{
			Person temp = returnResult.get(i);
			if(temp.getFirstName().concat(" " + temp.getLastName()).compareToIgnoreCase(LikeStr) != 0){		//concat firstname with lastname for comparison
				returnResult.remove(i);
				i--;
			}
		}
		//Now we check for referential Integrity.
		for(int i = 0; i < returnResult.size(); i++)
		{
			Person p; 
			int id = returnResult.get(i).getId();
			p = Person.find(id);
			FinalResult.add(p);
		}
		return FinalResult;
	}
	
	public ArrayList<Person> getPersonByID(int SearchID) throws SQLException, IOException, NotFoundException, ClassNotFoundException
	{
		ArrayList<Object> out = new ArrayList<Object>();
		ArrayList<Person> returnResult = new ArrayList<Person>();
		ArrayList<Person> FinalResult = new ArrayList<Person>();
		GetRow results = new GetRow();
		out = results.findObjects(ConfigData.PERSON_TABLE, "obj");
		//convert results from Objects to Model type
		for(int i = 0; i < out.size(); i++)
		{
			returnResult.add((Person) out.get(i));
		}
		//Now only get the records who have a match to "SearchID"
		for(int i = 0; i < returnResult.size(); i++)
		{
			Person temp = returnResult.get(i);
			if(temp.getId() != SearchID){
				returnResult.remove(i);
				i--;
			}
		}
		//Now we check for referential Integrity.
		for(int i = 0; i < returnResult.size(); i++)
		{
			Person p; 
			int id = returnResult.get(i).getId();
			p = Person.find(id);
			FinalResult.add(p);
		}
		return FinalResult;
	}
	
	public ArrayList<Person> getPersonByEmpID(String LikeStr) throws SQLException, IOException, NotFoundException, ClassNotFoundException
	{
		ArrayList<Object> out = new ArrayList<Object>();
		ArrayList<Person> returnResult = new ArrayList<Person>();
		ArrayList<Person> FinalResult = new ArrayList<Person>();
		GetRow results = new GetRow();
		out = results.findObjects(ConfigData.PERSON_TABLE, "obj");
		//convert results from Objects to Model type
		for(int i = 0; i < out.size(); i++)
		{
			returnResult.add((Person) out.get(i));
		}
		//Now only get the records who have a match to "LikeStr"
		for(int i = 0; i < returnResult.size(); i++)
		{
			Person temp = returnResult.get(i);
			if(temp.getEmployeeNumber().compareToIgnoreCase(LikeStr) != 0){
				returnResult.remove(i);
				i--;
			}
		}
		//Now we check for referential Integrity.
		for(int i = 0; i < returnResult.size(); i++)
		{
			Person p; 
			int id = returnResult.get(i).getId();
			p = Person.find(id);
			FinalResult.add(p);
		}
		return FinalResult;
	}
	
	public ArrayList<Person> getPersonByHiringDate(Date LikeDate) throws SQLException, IOException, NotFoundException, ClassNotFoundException
	{
		ArrayList<Object> out = new ArrayList<Object>();
		ArrayList<Person> returnResult = new ArrayList<Person>();
		ArrayList<Person> FinalResult = new ArrayList<Person>();
		GetRow results = new GetRow();
		out = results.findObjects(ConfigData.PERSON_TABLE, "obj");
		//convert results from Objects to Model type
		for(int i = 0; i < out.size(); i++)
		{
			returnResult.add((Person) out.get(i));
		}
		//Now only get the records who have a match to "LikeDate"
		for(int i = 0; i < returnResult.size(); i++)
		{
			Person temp = returnResult.get(i);
			if(!temp.getCreatedAt().equals(LikeDate)){
				returnResult.remove(i);
				i--;
			}
		}
		//Now we check for referential Integrity.
		for(int i = 0; i < returnResult.size(); i++)
		{
			Person p; 
			int id = returnResult.get(i).getId();
			p = Person.find(id);
			FinalResult.add(p);
		}
		return FinalResult;
	}
	
	public List<Person> getPersonByProjectID(int LikeID) throws NotFoundException, SQLException, IOException, ClassNotFoundException 
	{
		//TODO: we're mixing layers
		
		
		//only looking for a single project here, no array lists needed.
		Object out = new ArrayList<Object>();
		Project returnResult = new Project();
		GetRow results = new GetRow();
		out = results.findObjectById(LikeID, ConfigData.PROJECT_TABLE, "obj");
		//convert results from Object to Model type and return person list
		Project p = (Project) out;
		returnResult = Project.find(p.getId());
		return returnResult.getActors();
	}
	
	public ArrayList<Publication> getPublicationByTitle(String LikeStr) throws SQLException, IOException, NotFoundException, ClassNotFoundException
	{
		ArrayList<Object> out = new ArrayList<Object>();
		ArrayList<Publication> returnResult = new ArrayList<Publication>();
		ArrayList<Publication> FinalResult = new ArrayList<Publication>();
		GetRow results = new GetRow();
		out = results.findObjects(ConfigData.PUBLICATION_TABLE, "obj");
		//convert results from Objects to Model type
		for(int i = 0; i < out.size(); i++)
		{
			returnResult.add((Publication) out.get(i));
		}
		//Now only get the records who have a match to "LikeStr"
		for(int i = 0; i < returnResult.size(); i++)
		{
			Publication temp = returnResult.get(i);
			if(temp.getTitle().compareToIgnoreCase(LikeStr) != 0){
				returnResult.remove(i);
				i--;
			}
		}
		//Now we check for referential Integrity.
		for(int i = 0; i < returnResult.size(); i++)
		{
			Publication p; 
			int id = returnResult.get(i).getId();
			p = Publication.find(id);
			FinalResult.add(p);
		}
		return FinalResult;
	}
	
	public ArrayList<Publication> getPublicationByPublicationDate(Date LikeDate) throws SQLException, IOException, NotFoundException, ClassNotFoundException
	{
		ArrayList<Object> out = new ArrayList<Object>();
		ArrayList<Publication> returnResult = new ArrayList<Publication>();
		ArrayList<Publication> FinalResult = new ArrayList<Publication>();
		GetRow results = new GetRow();
		out = results.findObjects(ConfigData.PUBLICATION_TABLE, "obj");
		//convert results from Objects to Model type
		for(int i = 0; i < out.size(); i++)
		{
			returnResult.add((Publication) out.get(i));
		}
		//Now only get the records who have a match to "LikeDate"
		for(int i = 0; i < returnResult.size(); i++)
		{
			Publication temp = returnResult.get(i);
			if(!temp.getPublicationDate().equals(LikeDate)){
				returnResult.remove(i);
				i--;
			}
		}
		//Now we check for referential Integrity.
		for(int i = 0; i < returnResult.size(); i++)
		{
			Publication p; 
			int id = returnResult.get(i).getId();
			p = Publication.find(id);
			FinalResult.add(p);
		}
		return FinalResult;
	}
	
	public List<Publication> getPublicationByProjectID(int LikeID) throws NotFoundException, SQLException, IOException, ClassNotFoundException 
	{
		//only looking for a single project here, no array lists needed.
		Object out = new ArrayList<Object>();
		Project returnResult = new Project();
		GetRow results = new GetRow();
		out = results.findObjectById(LikeID, ConfigData.PUBLICATION_TABLE, "obj");
		//convert results from Objects to Model type and return person list
		Project p = (Project) out;
		returnResult = Project.find(p.getId());
		return returnResult.getPublications();
	}
	
	public List<Deliverable> getDeliverableByProjectId(int projectId) throws SQLException, IOException, NotFoundException, ClassNotFoundException
	{
		List<Object> out = null;
		List<Deliverable> returnResult = new ArrayList<Deliverable>();
		List<Deliverable> FinalResult = new ArrayList<Deliverable>();;
		GetRow results = new GetRow();
		out = results.findObjects(ConfigData.DELIVERABLE_TABLE, "obj");
		//convert results from Objects to Model type
		for(int i = 0; i < out.size(); i++)
		{
			returnResult.add((Deliverable) out.get(i));
		}
		//Now only get the records who have a match to "projectId"
		for(int i = 0; i < returnResult.size(); i++)
		{
			Deliverable temp = returnResult.get(i);
			if(temp.getProject().getId() != projectId){
				returnResult.remove(i);
				i--;
			}
		}
		//Now we check for referential Integrity.
		for(int i = 0; i < returnResult.size(); i++)
		{
			Deliverable d; 
			int id = returnResult.get(i).getId();
			d = Deliverable.find(id);
			FinalResult.add(d);
		}
		return FinalResult;
	}
	
	public ArrayList<Deliverable> getDeliverableByLastUpdate(Date LikeDate) throws SQLException, IOException, NotFoundException, ClassNotFoundException
	{
		ArrayList<Object> out = new ArrayList<Object>();
		ArrayList<Deliverable> returnResult = new ArrayList<Deliverable>();
		ArrayList<Deliverable> FinalResult = new ArrayList<Deliverable>();
		GetRow results = new GetRow();
		out = results.findObjects(ConfigData.DELIVERABLE_TABLE, "obj");
		//convert results from Objects to Model type
		for(int i = 0; i < out.size(); i++)
		{
			returnResult.add((Deliverable) out.get(i));
		}
		//Now only get the records who have a match to "LikeDate"
		for(int i = 0; i < returnResult.size(); i++)
		{
			Deliverable temp = returnResult.get(i);
			if(!temp.getUpdatedAt().equals(LikeDate)){
				returnResult.remove(i);
				i--;
			}
		}
		//Now we check for referential Integrity.
		for(int i = 0; i < returnResult.size(); i++)
		{
			Deliverable d; 
			int id = returnResult.get(i).getId();
			d = Deliverable.find(id);
			FinalResult.add(d);
		}
		return FinalResult;
	}
	
	public ArrayList<Deliverable> getDeliverableByCompletionDate(Date LikeDate) throws SQLException, IOException, NotFoundException, ClassNotFoundException
	{
		ArrayList<Object> out = new ArrayList<Object>();
		ArrayList<Deliverable> returnResult = new ArrayList<Deliverable>();
		ArrayList<Deliverable> FinalResult = new ArrayList<Deliverable>();
		GetRow results = new GetRow();
		out = results.findObjects(ConfigData.DELIVERABLE_TABLE, "obj");
		//convert results from Objects to Model type
		for(int i = 0; i < out.size(); i++)
		{
			returnResult.add((Deliverable) out.get(i));
		}
		//Now only get the records who have a match to "LikeDate"
		for(int i = 0; i < returnResult.size(); i++)
		{
			Deliverable temp = returnResult.get(i);
			if(temp.getStatus().toString().compareToIgnoreCase("Complete") == 0){
				if(!temp.getUpdatedAt().equals(LikeDate)){
					returnResult.remove(i);
					i--;
				}
			}
		}
		//Now we check for referential Integrity.
		for(int i = 0; i < returnResult.size(); i++)
		{
			Deliverable d; 
			int id = returnResult.get(i).getId();
			d = Deliverable.find(id);
			FinalResult.add(d);
		}
		return FinalResult;
	}
	
	public ArrayList<Deliverable> getDeliverableByStatus(String LikeStr) throws SQLException, IOException, NotFoundException, ClassNotFoundException
	{
		ArrayList<Object> out = new ArrayList<Object>();
		ArrayList<Deliverable> returnResult = new ArrayList<Deliverable>();
		ArrayList<Deliverable> FinalResult = new ArrayList<Deliverable>();
		GetRow results = new GetRow();
		out = results.findObjects(ConfigData.DELIVERABLE_TABLE, "obj");
		//convert results from Objects to Model type
		for(int i = 0; i < out.size(); i++)
		{
			returnResult.add((Deliverable) out.get(i));
		}
		//Now only get the records who have a match to "LikeStr"
		for(int i = 0; i < returnResult.size(); i++)
		{
			Deliverable temp = returnResult.get(i);
			if(temp.getStatus().toString().compareToIgnoreCase(LikeStr) != 0){
				returnResult.remove(i);
				i--;
			}
		}
		//Now we check for referential Integrity.
		for(int i = 0; i < returnResult.size(); i++)
		{
			Deliverable d; 
			int id = returnResult.get(i).getId();
			d = Deliverable.find(id);
			FinalResult.add(d);
		}
		return FinalResult;
	}
	
	public ArrayList<Project> getProjectByTitle(String LikeStr) throws SQLException, IOException, NotFoundException, ClassNotFoundException
	{
		ArrayList<Object> out = new ArrayList<Object>();
		ArrayList<Project> returnResult = new ArrayList<Project>();
		ArrayList<Project> FinalResult = new ArrayList<Project>();
		GetRow results = new GetRow();
		out = results.findObjects(ConfigData.PROJECT_TABLE, "obj");
		//convert results from Objects to Model type
		for(int i = 0; i < out.size(); i++)
		{
			returnResult.add((Project) out.get(i));
		}
		//Now only get the records who have a match to "LikeStr"
		for(int i = 0; i < returnResult.size(); i++)
		{
			Project temp = returnResult.get(i);
			if(temp.getName().compareToIgnoreCase(LikeStr) != 0){
				returnResult.remove(i);
				i--;
			}
		}
		for(int i = 0; i < returnResult.size(); i++)
		{
			Project p; 
			int id = returnResult.get(i).getId();
			p = Project.find(id);
			FinalResult.add(p);
		}
		return FinalResult;
	}
	
	public ArrayList<Project> getProjectByCompletionDate(Date LikeDate) throws SQLException, IOException, NotFoundException, ClassNotFoundException
	{
		ArrayList<Object> out = new ArrayList<Object>();
		ArrayList<Project> returnResult = new ArrayList<Project>();
		ArrayList<Project> FinalResult = new ArrayList<Project>();
		GetRow results = new GetRow();
		out = results.findObjects(ConfigData.PROJECT_TABLE, "obj");
		//convert results from Objects to Model type
		for(int i = 0; i < out.size(); i++)
		{
			returnResult.add((Project) out.get(i));
		}
		//Now only get the records who have a match to "LikeDate"
		for(int i = 0; i < returnResult.size(); i++)
		{
			Project temp = returnResult.get(i);
			if(!temp.getExpectedCompletionDate().equals(LikeDate)){
				returnResult.remove(i);
				i--;
			}
		}
		for(int i = 0; i < returnResult.size(); i++)
		{
			Project p; 
			int id = returnResult.get(i).getId();
			p = Project.find(id);
			FinalResult.add(p);
		}
		return FinalResult;
	}
	
	public ArrayList<Project> getProjectByFinalCompletionDate(Date LikeDate) throws SQLException, IOException, NotFoundException, ClassNotFoundException
	{
		ArrayList<Object> out = new ArrayList<Object>();
		ArrayList<Project> returnResult = new ArrayList<Project>();
		ArrayList<Project> FinalResult = new ArrayList<Project>();
		GetRow results = new GetRow();
		out = results.findObjects(ConfigData.PROJECT_TABLE, "obj");
		//convert results from Objects to Model type
		for(int i = 0; i < out.size(); i++)
		{
			returnResult.add((Project) out.get(i));
		}
		//Now only get the records who have a match to "LikeDate"
		for(int i = 0; i < returnResult.size(); i++)
		{
			Project temp = returnResult.get(i);
			if(!temp.getFinalCompletionDate().equals(LikeDate)){
				returnResult.remove(i);
				i--;
			}
		}
		for(int i = 0; i < returnResult.size(); i++)
		{
			Project p; 
			int id = returnResult.get(i).getId();
			p = Project.find(id);
			FinalResult.add(p);
		}
		return FinalResult;
	}
	
	public ArrayList<Project> getProjectByStatus(String LikeStr) throws SQLException, IOException, NotFoundException, ClassNotFoundException
	{
		ArrayList<Object> out = new ArrayList<Object>();
		ArrayList<Project> returnResult = new ArrayList<Project>();
		ArrayList<Project> FinalResult = new ArrayList<Project>();
		GetRow results = new GetRow();
		out = results.findObjects(ConfigData.PROJECT_TABLE, "obj");
		//convert results from Objects to Model type
		for(int i = 0; i < out.size(); i++)
		{
			returnResult.add((Project) out.get(i));
		}
		//Now only get the records who have a match to "LikeStr"
		for(int i = 0; i < returnResult.size(); i++)
		{
			Project temp = returnResult.get(i);
			if(temp.getStatus().toString().compareToIgnoreCase(LikeStr) != 0){
				returnResult.remove(i);
				i--;
			}
		}
		for(int i = 0; i < returnResult.size(); i++)
		{
			Project p; 
			int id = returnResult.get(i).getId();
			p = Project.find(id);
			FinalResult.add(p);
		}
		return FinalResult;
	}
	
	public ArrayList<Milestone> getMilestoneByTitle(String LikeStr) throws SQLException, IOException, NotFoundException, ClassNotFoundException
	{
		ArrayList<Object> out = new ArrayList<Object>();
		ArrayList<Milestone> returnResult = new ArrayList<Milestone>();
		ArrayList<Milestone> FinalResult = new ArrayList<Milestone>();
		GetRow results = new GetRow();
		out = results.findObjects(ConfigData.MILESTONE_TABLE, "obj");
		//convert results from Objects to Model type
		for(int i = 0; i < out.size(); i++)
		{
			returnResult.add((Milestone) out.get(i));
		}
		//Now only get the records who have a match to "LikeStr"
		for(int i = 0; i < returnResult.size(); i++)
		{
			Milestone temp = returnResult.get(i);
			if(temp.getTitle().compareToIgnoreCase(LikeStr) != 0){
				returnResult.remove(i);
				i--;
			}
		}
		for(int i = 0; i < returnResult.size(); i++)
		{
			Milestone m; 
			int id = returnResult.get(i).getId();
			m = Milestone.find(id);
			FinalResult.add(m);
		}
		return FinalResult;
	}
	
	public ArrayList<Milestone> getMilestoneByCompletionDate(Date LikeDate) throws SQLException, IOException, NotFoundException, ClassNotFoundException
	{
		ArrayList<Object> out = new ArrayList<Object>();
		ArrayList<Milestone> returnResult = new ArrayList<Milestone>();
		ArrayList<Milestone> FinalResult = new ArrayList<Milestone>();
		GetRow results = new GetRow();
		out = results.findObjects(ConfigData.MILESTONE_TABLE, "obj");
		//convert results from Objects to Model type
		for(int i = 0; i < out.size(); i++)
		{
			returnResult.add((Milestone) out.get(i));
		}
		//Now only get the records who have a match to "LikeDate"
		for(int i = 0; i < returnResult.size(); i++)
		{
			Milestone temp = returnResult.get(i);
			if(!temp.getCompletionDate().equals(LikeDate)){
				returnResult.remove(i);
				i--;
			}
		}
		for(int i = 0; i < returnResult.size(); i++)
		{
			Milestone m; 
			int id = returnResult.get(i).getId();
			m = Milestone.find(id);
			FinalResult.add(m);
		}
		return FinalResult;
	}
	
	public List<Milestone> getMilestoneByProject(int projectId) throws SQLException, IOException, NotFoundException, ClassNotFoundException
	{
		List<Object> out = null;
		List<Milestone> returnResult =  new ArrayList<Milestone>();
		List<Milestone> FinalResult = new ArrayList<Milestone>();
		GetRow results = new GetRow();
		out = results.findObjects(ConfigData.MILESTONE_TABLE, "obj");
		//convert results from Objects to Model type
		for(int i = 0; i < out.size(); i++)
		{
			returnResult.add((Milestone) out.get(i));
		}
		//Now only get the records who have a match to "projectId"
//TODO: what's going on here!?
		for(int i = 0; i < returnResult.size(); i++)
		{
			Milestone temp = returnResult.get(i);
			
			if(temp.getDeliverables().get(0).getProject().getId() != projectId){
				returnResult.remove(i);
				i--;
			}
		}
		for(int i = 0; i < returnResult.size(); i++)
		{
			Milestone m; 
			int id = returnResult.get(i).getId();
			m = Milestone.find(id);
			FinalResult.add(m);
		}
		return FinalResult;
	}
}
